package com.example.demo.control;

import com.example.demo.entity.Appointment;
import com.example.demo.repository.AppointmentRepository;
import com.example.demo.utils.AppointmentType;
import com.example.demo.utils.BackResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentRepository appointmentRepository;
    /*
     * 获取预约状态信息
     * */
    @GetMapping("/AppointmentType")
    public Object getAppointmentType(){
        List<Map<String, String>> data = AppointmentType.getAppointmentType();
        return BackResult.success(data);
    }
    /*
    * 获取个人预约信息
    * */
    @GetMapping("/findOneAppointment")
    public Object findAppendable(Integer id){
        List<Map<String, Object>> appointment=appointmentRepository.query(id);
        if (id==null){
            return BackResult.fail("id不能为空！");
        }
        if (appointment==null){
            return BackResult.fail("没有预约信息！");
        }
        List<Map<String, Object>> data = appointmentRepository.query(id);
        return  BackResult.success(data);
    }
    /*
    * 获取所有用户预约信息
    * */
    @GetMapping("/findAllAppointment")
    public  Object findAll(Integer page,Integer pageSize){
        if (pageSize==null||page==null) {
            page=0;
            pageSize=15;
        }
        List<Map<String, Object>> list = appointmentRepository.find(pageSize * page, pageSize);
         int total=appointmentRepository.total();
        Map<String,Object> map =new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return  BackResult.success(map);
    }

    @GetMapping("/findAllAppointmentD")
    public  Object findAllDoctor(Integer page,Integer pageSize,Integer id){
        if (pageSize==null||page==null) {
            page=0;
            pageSize=15;
        }
        List<Map<String, Object>> list = appointmentRepository.findByType(pageSize * page, pageSize,id);
        int total=appointmentRepository.total();
        Map<String,Object> map =new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return  BackResult.success(map);
    }
    /*
    * 挂号
    * */
    @PostMapping("/addAppointment")
    public Object addAppointment(Appointment appointment,String date,String doctor,Integer outpatientid,Integer state,Integer u_id){
        System.out.println("".equals(date)&&"".equals(doctor));
        System.out.println(doctor);
        if (u_id<=0){
            System.out.println(u_id);
            return  BackResult.fail("预约人不能为空！");
        }
        if ((date!=null &&doctor!=null) && ("".equals(date) &&"".equals(doctor))){
            System.out.println("->");
            return BackResult.fail("预约时间和医生不能同时为空！");
        }
        if (!(outpatientid!=null&&outpatientid>0)){
            return BackResult.fail("预约门诊不能为空！");
        }
        if (state<=0){
            return BackResult.fail("预约状态不能为空！");
        }

        appointment.set_live(1);
        appointment.setState(state);
        appointment.setOutpatientid(outpatientid);
        Appointment appointment1=appointmentRepository.save(appointment);
        if (appointment1!=null){
            return  BackResult.success(appointment1);
        }else {
            return BackResult.fail("添加失败！");
        }
    }

    /*
     * 修改预约信息
     * */
    @PostMapping("/updateAppointment")
    public Object updateAppointment(Integer id,String date,Integer doctor,Integer outpatientid,Integer state,String type){
      Appointment appointment=  appointmentRepository.findByIds(id);
      if (appointment==null){
          return BackResult.fail("预约已锁定!");
      }
      System.out.println(appointment);
      if (date!=null&&appointment.getDate()==null){
          appointment.setDate(date);
          System.out.println(date);
      }
      if (appointment.getOutpatientid()<=0){
          return BackResult.fail("门诊不能为空！");
      }
      if (doctor!=null){
          appointment.setDoctor(doctor);
      }
      if (outpatientid!=null){
          appointment.setOutpatientid(outpatientid);
      }
      if (state!=null){
          appointment.setState(state);
      }
      if("date".equals(type)){
          appointment.setDoctor(0);
      }else {
          appointment.setDate(null);
      }
      appointment.set_live(1);
      appointment.setType(type);
      appointmentRepository.save(appointment);
      return BackResult.success(appointment);
    }

    @PostMapping("/remove")
    public Object remove(Integer id){
//     Appointment appointment=appointmentRepository.findById1(ids);
//        System.out.println(appointment);
       try {
           appointmentRepository.deleteById(id);
           return BackResult.success();
       }catch (Exception e) {
           return BackResult.fail(e.getMessage());
       }
    }

    @GetMapping("/findState")
    public Object findByState(Integer page,Integer pageSize){
        if (pageSize==null||page==null) {
            page=0;
            pageSize=15;
        }
        List<Map<String, Object>> list = appointmentRepository.findById1(pageSize * page, pageSize);
        int total=appointmentRepository.total();
        Map<String,Object> map =new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return  BackResult.success(map);
    }


    @PostMapping("/updateAppointment1")
    public Object updateAppointment1(Integer id){
        Appointment appointment=  appointmentRepository.findByIds(id);
        if (appointment==null){
            return BackResult.fail("预约信息不存在!");
        }
        appointment.set_live(1);
        appointment.setState(2);
        appointmentRepository.save(appointment);
        return BackResult.success(appointment);
    }

    @PostMapping("/updateAppointment2")
    public Object updateAppointment2(Integer id){
        Appointment appointment=  appointmentRepository.findByIds(id);
        if (appointment==null){
            return BackResult.fail("预约信息不存在!");
        }
        appointment.set_live(1);
        appointment.setState(3);
        appointmentRepository.save(appointment);
        return BackResult.success(appointment);
    }
}
